package cn.iocoder.yudao.module.scrm.controller.admin.phoneencryptor;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.scrm.controller.admin.phoneencryptor.vo.*;
import cn.iocoder.yudao.module.scrm.dal.dataobject.phoneencryptor.PhoneEncryptorDO;
import cn.iocoder.yudao.module.scrm.service.phoneencryptor.PhoneEncryptorService;

@Tag(name = "管理后台 - 客户手机加密配置")
@RestController
@RequestMapping("/scrm/phone-encryptor")
@Validated
public class PhoneEncryptorController {

    @Resource
    private PhoneEncryptorService phoneEncryptorService;

    @PostMapping("/create")
    @Operation(summary = "创建客户手机加密配置")
    @PreAuthorize("@ss.hasPermission('scrm:phone-encryptor:create')")
    public CommonResult<Boolean> createPhoneEncryptor(@Valid @RequestBody PhoneEncryptorSaveReqVO createReqVO) {
        return success(phoneEncryptorService.createPhoneEncryptor(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户手机加密配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:phone-encryptor:delete')")
    public CommonResult<Boolean> deletePhoneEncryptor(@RequestParam("id") Long id) {
        phoneEncryptorService.deletePhoneEncryptor(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户手机加密配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:phone-encryptor:query')")
    public CommonResult<PhoneEncryptorRespVO> getPhoneEncryptor(@RequestParam("id") Long id) {
        PhoneEncryptorDO phoneEncryptor = phoneEncryptorService.getPhoneEncryptor(id);
        return success(BeanUtils.toBean(phoneEncryptor, PhoneEncryptorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户手机加密配置分页")
    @PreAuthorize("@ss.hasPermission('scrm:phone-encryptor:query')")
    public CommonResult<PageResult<PhoneEncryptorRespVO>> getPhoneEncryptorPage(@Valid PhoneEncryptorPageReqVO pageReqVO) {
        PageResult<PhoneEncryptorDO> pageResult = phoneEncryptorService.getPhoneEncryptorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PhoneEncryptorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户手机加密配置 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:phone-encryptor:export')")
    @OperateLog(type = EXPORT)
    public void exportPhoneEncryptorExcel(@Valid PhoneEncryptorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PhoneEncryptorDO> list = phoneEncryptorService.getPhoneEncryptorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户手机加密配置.xls", "数据", PhoneEncryptorRespVO.class,
                        BeanUtils.toBean(list, PhoneEncryptorRespVO.class));
    }

}